//
//  HDWaitingTableViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/13.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDWaitingTableViewController.h"
#import "HDStartLearningViewController.h"

@interface HDWaitingTableViewController ()

@end

@implementation HDWaitingTableViewController

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    HDWaitingCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (cell==nil) {
        cell = [[HDWaitingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.orderType = self.orderType;
    }
    cell.customerInfo = self.dataArray[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [HDWaitingCell cellHeight:self.dataArray[indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==self.dataArray.count-1) {
        return 10;
    }
    return 0.01;
}

@end

@interface HDWaitingCell ()
@property(nonatomic,strong)DOImageView *photo;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)UILabel *stationTimeLabel;
@property(nonatomic,strong)UILabel *typeLabel;
@property(nonatomic,strong)UILabel *learnTimeLabel;
@property(nonatomic,strong)UILabel *remarkLabel;
@property(nonatomic,strong)GXButton *callBtn;
@property(nonatomic,strong)GXButton *needCompanyBtn;
@property(nonatomic,strong)UIView *startLearnView;
@property(nonatomic,strong)UILabel *subjectLabel;
@property(nonatomic,strong)UIButton *startBtn;
@property(nonatomic,strong)NSTimer* timer;
@end

@implementation HDWaitingCell
@synthesize startBtn;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)dealloc
{
    if(self.timer!=nil)
    {
        [self.timer invalidate];
        self.timer=nil;
    }
}



- (void)initUI
{
    UIImageView *line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
    line1.image = [UIImage imageNamed:@"dot"];
    [self.contentView addSubview:line1];
    [self.contentView addSubview:self.photo];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.stationTimeLabel];
    [self.contentView addSubview:self.typeLabel];
    [self.contentView addSubview:self.subjectLabel];
    UIImageView *line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.photo.bottom+20, APPFRAME.size.width, 0.5)];
    line2.image = [UIImage imageNamed:@"dot"];
    [self.contentView addSubview:line2];
    [self.contentView addSubview:self.learnTimeLabel];
    [self.contentView addSubview:self.remarkLabel];
    UIImageView *line3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.callBtn.left-5, 20, 0.5, 45)];
    line3.image = [UIImage imageNamed:@"dot"];
    [self.contentView addSubview:line3];
    [self.contentView addSubview:self.callBtn];
    [self.contentView addSubview:self.needCompanyBtn];
    [self.contentView addSubview:self.startLearnView];
}

+ (CGFloat)cellHeight:(HDCustomerInfo *)customerInfo
{
    if (customerInfo.orderType==HDOrderTypePractice) {
        CGFloat remarkLabelWidth = customerInfo.needCompany?APPFRAME.size.width-95:APPFRAME.size.width-20;
        CGSize size = [HDUtil sizeWithString:[NSString stringWithFormat:@"备注：%@",customerInfo.remark] font:[UIFont systemFontOfSize:13] size:CGSizeMake(remarkLabelWidth, CGFLOAT_MAX)];
        if (size.height>15) {
            return 195+size.height;
        }
    }
    return 210;
}

- (void)setCustomerInfo:(HDCustomerInfo *)value
{
    _customerInfo = [value copy];
    self.photo.imgURLStr = _customerInfo.photoUrl;
    self.nameLabel.text = _customerInfo.name;
    self.stationTimeLabel.text = [NSString stringWithFormat:@"平台学时：%@",_customerInfo.stationTime];
    self.typeLabel.text = [NSString stringWithFormat:@"驾考类型：%@",_customerInfo.type];
    self.learnTimeLabel.text = @"学车时间：";
    if (_customerInfo.startTime!=nil&&_customerInfo.endTime!=nil)
    {
        NSString *date = [HDUtil formatter:@"M月d日" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *startTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.startTime doubleValue]];
        NSString *endTime = [HDUtil formatter:@"HH:mm" FromTime:[_customerInfo.endTime doubleValue]];
        self.learnTimeLabel.text = [NSString stringWithFormat:@"学车时间：%@ %@-%@ %@个小时",date,startTime,endTime,[HDUtil check:_customerInfo.learnTime]];
    }
    self.needCompanyBtn.hidden = !_customerInfo.needCompany;
    CGFloat remarkLabelWidth = _customerInfo.needCompany?APPFRAME.size.width-95:APPFRAME.size.width-20;
    if (self.orderType==HDOrderTypePractice)
    {
        self.remarkLabel.numberOfLines = 0;
        NSString *remarkStr = [NSString stringWithFormat:@"备注：%@",_customerInfo.remark];
        self.remarkLabel.text = remarkStr;
        CGSize size = [HDUtil sizeWithString:remarkStr font:[UIFont systemFontOfSize:13] size:CGSizeMake(remarkLabelWidth, CGFLOAT_MAX)];
        self.remarkLabel.height = size.height>15?size.height:15;
        self.subjectLabel.hidden = YES;
    }
    else
    {
        self.remarkLabel.width = remarkLabelWidth;
        self.remarkLabel.numberOfLines = 1;
        self.remarkLabel.text = [NSString stringWithFormat:@"训练场地：%@",_customerInfo.practicePosition];
        self.subjectLabel.hidden = NO;
        self.subjectLabel.text = _customerInfo.subject;
    }
    self.startLearnView.top = self.remarkLabel.bottom+15;
    NSString *startBtnTitle = _customerInfo.orderStatus;
    startBtnTitle = [_customerInfo.orderStatus stringByReplacingOccurrencesOfString:@"已抢单" withString:@"开始上课"];
    [startBtn setTitle:startBtnTitle forState:UIControlStateNormal];
    
    if([startBtnTitle rangeOfString:@"开始"].location!=NSNotFound)
    {
        [startBtn setBackgroundImage:[UIImage imageNamed:@"开始上课"] forState:UIControlStateNormal];
    }
    else
    {
        [startBtn setBackgroundImage:[UIImage imageNamed:@"进行中"] forState:UIControlStateNormal];
    }
    [self checkBtnState];
    
}


-(void)checkBtnState
{
    if(self.timer!=nil)
    {
        [self.timer invalidate];
        self.timer=nil;
    }
    
    startBtn.enabled=NO;
    NSDate* date=[NSDate date];
    double now=[date timeIntervalSince1970];
    
    if (now<[_customerInfo.startTime doubleValue])
    {
        startBtn.enabled=YES;
        return;
    }
    else if (now<([_customerInfo.startTime doubleValue]+3600))
    {
        startBtn.enabled=YES;
        return;
    }
    
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkBtnState) userInfo:nil repeats:NO];
}

- (void)startLearning:(UIButton *)sender
{
    HDStartLearningViewController *startLearningVC = [[HDStartLearningViewController alloc] init];
    startLearningVC.navigationItem.title = @"开始上课";
    startLearningVC.orderType = self.orderType;
    startLearningVC.customerInfo = self.customerInfo;
    [rootNavigation pushViewController:startLearningVC animated:YES];
}

- (void)callAction:(UIButton *)sender
{
    [HDUtil call:[HDUtil check:self.customerInfo.phoneNumber]];
}

#pragma mark - getters
- (DOImageView *)photo
{
    if (!_photo) {
        _photo = [[DOImageView alloc] initWithFrame:CGRectMake(10, 15, 50, 50)];
        _photo.layer.cornerRadius = 5;
        _photo.layer.masksToBounds = YES;
        _photo.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _photo;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, 15, APPFRAME.size.width-130, 20)];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        _nameLabel.textColor = kDarkGrayColor;
    }
    return _nameLabel;
}

- (UILabel *)stationTimeLabel
{
    if (!_stationTimeLabel) {
        _stationTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.nameLabel.bottom, APPFRAME.size.width-130, 15)];
        _stationTimeLabel.font = [UIFont systemFontOfSize:12];
        _stationTimeLabel.textColor = [UIColor hexStringToColor:@"#424242"];
    }
    return _stationTimeLabel;
}

- (UILabel *)typeLabel
{
    if (!_typeLabel) {
        _typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.photo.right+15, self.stationTimeLabel.bottom, APPFRAME.size.width-130, 15)];
        _typeLabel.font = [UIFont systemFontOfSize:12];
        _typeLabel.textColor = kLightGrayColor;
    }
    return _typeLabel;
}

- (UILabel *)learnTimeLabel
{
    if (!_learnTimeLabel) {
        _learnTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.photo.bottom+30, APPFRAME.size.width-20, 15)];
        _learnTimeLabel.font = [UIFont systemFontOfSize:13];
        _learnTimeLabel.textColor = kDarkGrayColor;
    }
    return _learnTimeLabel;
}

- (UILabel *)remarkLabel
{
    if (!_remarkLabel) {
        _remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.learnTimeLabel.bottom+10, APPFRAME.size.width-95, 15)];
        _remarkLabel.font = [UIFont systemFontOfSize:13];
        _remarkLabel.textColor = kDarkGrayColor;
    }
    return _remarkLabel;
}

- (GXButton *)callBtn
{
    if (!_callBtn) {
        _callBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _callBtn.frame = CGRectMake(APPFRAME.size.width-50, 20, 40, 40);
        _callBtn.imageViewFrame = CGRectMake(2.5, 2.5, 35, 35);
        _callBtn.relayout = YES;
        [_callBtn setImage:[UIImage imageNamed:@"电话"] forState:UIControlStateNormal];
        [_callBtn addTarget:self action:@selector(callAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _callBtn;
}

- (GXButton *)needCompanyBtn
{
    if (!_needCompanyBtn) {
        _needCompanyBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _needCompanyBtn.frame = CGRectMake(APPFRAME.size.width-83, self.learnTimeLabel.bottom+5, 73, 25);
        _needCompanyBtn.imageViewFrame = CGRectMake(2, 5, 19, 14);
        _needCompanyBtn.titleLabelFrame = CGRectMake(23, 0, 50, 25);
        _needCompanyBtn.relayout = YES;
        [_needCompanyBtn setTitle:@"需要接送" forState:UIControlStateNormal];
        [_needCompanyBtn setImage:[UIImage imageNamed:@"汽车.jpg"] forState:UIControlStateNormal];
        [_needCompanyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _needCompanyBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        _needCompanyBtn.userInteractionEnabled = NO;
        _needCompanyBtn.hidden = YES;
    }
    return _needCompanyBtn;
}

- (UIView *)startLearnView
{
    if (!_startLearnView) {
        _startLearnView = [[UIView alloc] initWithFrame:CGRectMake(0, self.remarkLabel.bottom+15, APPFRAME.size.width, 60)];
        UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 0.5)];
        topLine.image = [UIImage imageNamed:@"dot"];
        [_startLearnView addSubview:topLine];
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, APPFRAME.size.width, 0.5)];
        bottomLine.image = [UIImage imageNamed:@"dot"];
        [_startLearnView addSubview:bottomLine];
        self.startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        startBtn.frame = CGRectMake((APPFRAME.size.width-200)/2.0, 10, 200, 40);
        [startBtn setTitle:@"开始上课" forState:UIControlStateNormal];
        [startBtn setBackgroundImage:[UIImage imageNamed:@"开始上课"] forState:UIControlStateNormal];
        [startBtn setBackgroundImage:[UIImage imageNamed:@"拒绝"] forState:UIControlStateDisabled];
        startBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [startBtn addTarget:self action:@selector(startLearning:) forControlEvents:UIControlEventTouchUpInside];
        startBtn.enabled=NO;
        [_startLearnView addSubview:startBtn];
    }
    return _startLearnView;
}

- (UILabel *)subjectLabel
{
    if (!_subjectLabel) {
        _subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(APPFRAME.size.width-115, 35, 50, 20)];
        _subjectLabel.font = [UIFont systemFontOfSize:14];
        _subjectLabel.textColor = kDarkGrayColor;
    }
    return _subjectLabel;
}

@end
